package com.fowo.api.service.handleWork;

import com.fowo.api.entity.StockInProduct;
import com.fowo.api.model.payable.PayableVo;
import com.fowo.api.model.payable.product.PayableProductItemVo;
import com.fowo.api.model.process.product.ProcessProductPushapAction;
import com.fowo.api.model.process.product.ProcessProductVo;
import com.fowo.api.model.process.product.item.ProcessProductItemItemVo;
import com.fowo.api.model.stock.in.StockInPushprocessAction;
import com.fowo.api.service.ProcessProductService;
import com.fowo.api.service.impl.PayableProductServiceImpl;
import com.fowo.api.service.impl.PayableServiceImpl;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class ProcessProductPushapActionimpl implements ProcessProductPushapAction {

    @Resource
    private ProcessProductService processProductService;
    @Resource
    private PayableServiceImpl payableService;

    @Autowired(required = false)
    protected StockInPushprocessAction stockInPushprocessAction;

    @Resource
    private PayableProductServiceImpl payableProductService;
    @Transactional
    @Override
    public void run(List<Long> idList) throws Exception {
        for(int i=0;i<idList.size();i++){
            ProcessProductVo voById = processProductService.getVoById(idList.get(i));
            Assert.isTrue(voById.getStatus().equals("1"),"违规操作");
            Assert.isTrue(voById.getGenerateAp().equals("0"),"违规操作");
            handleData(voById);
        }
    }

    private void handleData(ProcessProductVo voById) throws Exception {
        JwtUserInfo currentUser = JwtUserInfo.fromHeader();
        PayableVo model =new PayableVo();
        model.setStauts("0");
        model.setPayOrg(1646772548192239617L);
        model.setPurchasesOrg(1646772548192239617L);
        model.setSettlementOrg(1646772548192239617L);
        //截取入库单号
        String po=voById.getRemarks();
        String remarks = voById.getRemarks() == null ? "" : voById.getRemarks()+",";
        model.setRemarks(remarks+"加工单号:"+voById.getProcessSn());
        model.setBusinessDate(voById.getPdate());
        model.setSupplierId(voById.getSupplier());
        model.setProductType("1");
        model.setPushStatus("0");
        model.setWarehouse(voById.getWid());
//        model.setSettlementDate(stockIn.getWarehousingDate());
        model.setPreparedBy(currentUser.getUserId());
//        model.setPurchaseOrder(getOrder(stockIn.getRemarks())==null?stockIn.getPurchaseOrderCode():getOrder(stockIn.getRemarks()));
//        model.setWarehouseSn(stockIn.getOrderSn());
        List<PayableProductItemVo> productltem=new ArrayList<>();
        for(int i=0;i<voById.getProcessProductItems().size();i++){
            ProcessProductItemItemVo processProductItemItemVo = voById.getProcessProductItems().get(i);
            PayableProductItemVo payableProductItemVo=new PayableProductItemVo();
            payableProductItemVo.setNum( processProductItemItemVo.getNum().intValue());
            payableProductItemVo.setPrice(processProductItemItemVo.getProcessFee());
            payableProductItemVo.setName(processProductItemItemVo.getProductIdName());
            payableProductItemVo.setProductId(processProductItemItemVo.getProductId());
            payableProductItemVo.setSku(processProductItemItemVo.getSku());
            payableProductItemVo.setSubtotal(processProductItemItemVo.getSubtotal());
            productltem.add(payableProductItemVo);
        }
        model.setProductltem(productltem);
        try {
            payableService.create(model);
        } catch (Exception e) {
            throw new RuntimeException("");
        }
        voById.setGenerateAp("2");
        processProductService.update(voById);


    }
}
